#LD := ld -m elf_x86_64
USE_DEBUG :=
GCC := gcc
#CCFLAG := -mcpu=i386 -I../include -nostdinc -Wall -fomit-frame-pointer -c -m32
CCFLAG := -I../include -nostdinc -ffreestanding -Wall -fomit-frame-pointer -fno-pic -fno-stack-protector -c -m32 $(USE_DEBUG)
LDFLAG := -Ttext 0x0 -s --oformat binary -m elf_i386
INCDIR := ../include
OBJS := head.o main.o sched.o fork.o printm.o vsprintf.o sys_call.o panic.o chr_drv/chr_drv.a ../mm/mm.o mystdio/mystdio traps.o asm/asm ../lib/lib.a

system: $(OBJS)
	$(LD) $(LDFLAG) -e startup_32 -o $@ $^ -M > System.map

head.o : head.S
	$(GCC) -m32 -traditional -c -o $@ $<

main.o : main.c
	$(GCC) $(CCFLAG) -o $@ $<

sched.o : sched.c
	$(GCC) $(CCFLAG) -o $@ $<

printm.o : printm.c
	$(GCC) $(CCFLAG) -o $@ $<

vsprintf.o : vsprintf.c
	$(GCC) $(CCFLAG) -o $@ $<

traps.o : traps.c
	$(GCC) $(CCFLAG) -o $@ $<

sys_call.o : sys_call.S
	$(GCC) -m32 -traditional -c -o $@ $<

chr_drv/chr_drv.a: chr_drv/*.c
	cd chr_drv; make chr_drv.a; cd ..

../mm/mm.o : ../mm/*.c
	cd ../mm; make mm.o; cd ../kernel
	
mystdio/mystdio : mystdio/*.c mystdio/*.S
	cd mystdio; make mystdio; cd ..

fork.o : fork.c
	$(GCC) $(CCFLAG) -o $@ $<

panic.o : panic.c
	$(GCC) $(CCFLAG) -o $@ $<

asm/asm : asm/io.S asm/asm.S
	cd asm; make asm; cd ..

../lib/lib.a : ../lib/*.c
	cd ../lib; make lib.a; cd ../kernel

clean :
	rm -f *.o system
	rm -f System.map
	cd chr_drv; make clean; cd ..
	cd ../mm; make clean; cd ../kernel
	cd mystdio; make clean; cd ..
	cd asm; make clean; cd ..
	cd ../lib; make clean; cd ../kernel
